﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CE.User;
using CE.Global;
using CE.Provider;

public partial class Provider_CarInsurance_ProviderCarPolicyInfo : System.Web.UI.Page
{
    string PageName = "CXCI";
    string SubPageName = "CXCIPLINFO";
    public static Int64 _providercarpolicyinfoid = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null || Session[CE.Global.General.ProviderCompanyId] == null)
        {
            Response.Redirect("~/Provider/ProviderSecureLogin.aspx");
        }
        CheckMainPageAccess();
        CheckSubPageAccess();

        if (!IsPostBack)
        {
            ClearProcess();
            
            
            if (Request.Params["PRID"] != null)
            {
                Int32 _CarPolicyInfoid;
                if (Int32.TryParse(Request.Params["PRID"].ToString(), out _CarPolicyInfoid))
                {
                    ShowCarPolicyInfo(_CarPolicyInfoid);
                }
            }
        }
    }

    private void ShowCarPolicyInfo(Int64 ProviderCarPolicyInfoId)
    {
        CarPolicyInfo _CarPolicyInfo = new CarPolicyInfo();
        CarPolicyInfoDB _CarPolicyInfodb = new CarPolicyInfoDB();

        _CarPolicyInfo = _CarPolicyInfodb.GetProviderCarPolicyInfo(ProviderCarPolicyInfoId);

        if (_CarPolicyInfo != null)
        {
            _providercarpolicyinfoid = Convert.ToInt64(_CarPolicyInfo.ProviderCarPolicyId.ToString());
            lblProviderCarPolicyInfoId.Text = _CarPolicyInfo.ProviderCarPolicyId.ToString();
            txtPolicyInfoTitle.Text = _CarPolicyInfo.PolicyInfoTitle;
            //txtPersonalAccidentLimit.Text = _CarPolicyInfo.PersonalAccidentLimit.ToString();
            txtWindscreenAccess.Text = _CarPolicyInfo.WindscreenAccess.ToString();
            txtTowingCharges.Text = _CarPolicyInfo.TowingCharges.ToString();
            txtExpiry.Text = _CarPolicyInfo.ExcessUnnamedDriver.ToString(); //unnamed to expiry days..
            txtCancellationPenalty.Text = _CarPolicyInfo.CancellationPenalty.ToString();
            chkStatus.Checked = Convert.ToBoolean(_CarPolicyInfo.Status);

        }
    }

    public void ClearProcess()
    {
        _providercarpolicyinfoid = 0;
        lblProviderCarPolicyInfoId.Text = "0";
        txtPolicyInfoTitle.Text = "";
        //txtPersonalAccidentLimit.Text = "";
        txtWindscreenAccess.Text = "";
        txtTowingCharges.Text = "";
        txtExpiry.Text = "";
        txtCancellationPenalty.Text = "";
        chkStatus.Checked = true;
        lblMessage.Text = "";

    }

    protected void btnClearForm_Click(object sender, EventArgs e)
    {
        ClearProcess();
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        
        if (Page.IsValid)
        {
            string _authcode = CE.Security.MySecurity.Encrypt(txtAuthCode.Text, true);
            if (CheckAuthCode(_authcode, 100))
            {
                CarPolicyInfo _CarPolicyInfo = new CarPolicyInfo();
                CarPolicyInfoDB _CarPolicyInfodb = new CarPolicyInfoDB();

                int _trantype = 1;
                _CarPolicyInfo.ProviderCarPolicyId = Convert.ToInt64(lblProviderCarPolicyInfoId.Text);
                if (_CarPolicyInfo.ProviderCarPolicyId > 0)
                {
                    _trantype = 2;
                }
                _CarPolicyInfo.ProviderCompanyId = Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId].ToString());
                _CarPolicyInfo.PolicyInfoTitle = txtPolicyInfoTitle.Text.Trim();
                _CarPolicyInfo.PersonalAccidentLimit = 0;
                _CarPolicyInfo.WindscreenAccess = Convert.ToInt64(txtWindscreenAccess.Text);
                _CarPolicyInfo.TowingCharges = Convert.ToInt64(txtTowingCharges.Text);                
                _CarPolicyInfo.ExcessUnnamedDriver = Convert.ToInt64(txtExpiry.Text);
                _CarPolicyInfo.QuoteExpiryDate = CE.Global.General.GetUTCDateTime();
                _CarPolicyInfo.CancellationPenalty = Convert.ToInt64(txtCancellationPenalty.Text);
                _CarPolicyInfo.RefCEUserId = Convert.ToInt64(Session[CE.Global.General.UserId].ToString());
                _CarPolicyInfo.Status = Convert.ToBoolean(chkStatus.Checked.ToString());
                _CarPolicyInfo.ProviderCarPolicyId = _CarPolicyInfodb.Insert(_CarPolicyInfo);

                if (_CarPolicyInfo.ProviderCarPolicyId > 0)
                {
                    ClearProcess();
                    InsertProviderAuthTransaction(_CarPolicyInfo.ProviderCarPolicyId, "CICarPolicyInfo", _trantype, 100, _authcode, _CarPolicyInfo.ProviderCompanyId, _CarPolicyInfo.RefCEUserId);
                    lblMessage.Text = "Saved successfully!";
                }
                else
                {
                    lblMessage.Text = "Saving failed!";
                }
            }
            else
            {
                lblMessage.Text = "Wrong auth code!";
            }
        }
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        rfvAuthCode.Validate();
        if (lblProviderCarPolicyInfoId.Text != "0" && txtAuthCode.Text.Trim().Length > 0)
        {
            Int64 _providercompanyid = Convert.ToInt64(Session[General.ProviderCompanyId].ToString());
            string _authcode = CE.Security.MySecurity.Encrypt(txtAuthCode.Text, true);
            Int64 _refceuserid = Convert.ToInt64(Session[General.UserId].ToString());
            if (CheckAuthCode(_authcode, 100))
            {

                CarPolicyInfoDB _CarPolicyInfodb = new CarPolicyInfoDB();
                if (_CarPolicyInfodb.Delete(Convert.ToInt64(lblProviderCarPolicyInfoId.Text), _providercompanyid))
                {
                    InsertProviderAuthTransaction(Convert.ToInt64(lblProviderCarPolicyInfoId.Text), "CICarPolicyInfo", 3, 100, _authcode, _providercompanyid, _refceuserid);
                    Response.Redirect("ViewProviderCarPolicyInfo.aspx");
                }
                else
                {
                    lblMessage.Text = "Deleting failed.";
                }

            }
            else
            {
                lblMessage.Text = "Wrong auth code!";
            }
        }

    }

    protected void ibtBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("ViewProviderCarPolicyInfo.aspx");
    }


    protected bool CheckAuthCode(string strAuthCode, int intAuthCodeType)
    {
        bool _retValue = false;
        ProviderAuthCode _authcode = new ProviderAuthCode();
        ProviderAuthCodeDB _authcodedb = new ProviderAuthCodeDB();
        _authcode = _authcodedb.GetProviderAuthCode(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()), intAuthCodeType, strAuthCode);
        if (_authcode != null)
        {
            _retValue = true;
        }

        return _retValue;
    }

    protected Int64 InsertProviderAuthTransaction(Int64 TransactionId, string TransactionCode, int TransactionType,
        int AuthCodeType, string AuthCode, Int64 ProviderCompanyId, Int64 RefCEUserId)
    {
        ProviderAuthTransaction _authtrans = new ProviderAuthTransaction();
        ProviderAuthTransactionDB _authtransdb = new ProviderAuthTransactionDB();

        _authtrans.TransactionId = TransactionId;
        _authtrans.TransactionCode = TransactionCode;
        _authtrans.TransactionType = TransactionType;
        _authtrans.AuthCode = AuthCode;
        _authtrans.AuthCodeType = AuthCodeType;
        _authtrans.ProviderCompanyId = ProviderCompanyId;
        _authtrans.RefCEUserID = RefCEUserId;

        _authtrans.AuthTransId = _authtransdb.Insert(_authtrans);
        return _authtrans.AuthTransId;
    }

    private void CheckMainPageAccess()
    {
        Int64 PageAccess = General.GetProviderMainPageAccess(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),
                                                        Convert.ToInt64(Session[General.UserId].ToString()), PageName);
        if (PageAccess == 0)
        {
            //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
            Response.Redirect("~/Provider/UserMessage.aspx?mt=UM&ec=2000");
        }
    }

    private void CheckSubPageAccess()
    {
        Int64 PageAccess = General.GetProviderSubPageAccess(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),
                                                        Convert.ToInt64(Session[General.UserId].ToString()), SubPageName);
        if (PageAccess == 0)
        {
            //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
            Response.Redirect("~/Provider/UserMessage.aspx?mt=UM&ec=2000");
        }
        else if (PageAccess == 100)
        {
            ibtSubmit.Enabled = false;
            btnNew.Enabled = false;
            btnDelete.Enabled = false;
        }
        else if (PageAccess == 200)
        {
            ibtSubmit.Enabled = true;
            btnNew.Enabled = true;
            btnDelete.Enabled = false;
        }
        else if (PageAccess == 300)
        {
            ibtSubmit.Enabled = true;
            btnNew.Enabled = true;
            btnDelete.Enabled = true;
        }
    }

}
